package com.huidu.bitrade.listener;

import cn.hutool.core.util.RandomUtil;
import com.huidu.bitrade.server.KafkaClient;
import com.huidu.bitrade.server.MongodbClient;
import com.huidu.bitrade.server.MysqlClient;
import com.huidu.bitrade.vendor.provider.SMSProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class BasicServerListener {
    private final MysqlClient mysqlClient;
    private final MongodbClient mongodbClient;
    private final KafkaClient kafkaClient;
    private final SMSProvider smsProvider;

    private List<String> notifyList;//发送短信手机号

    @Value("${server.monitor.basic.flag}")
    private Integer flag;

    private int errorCountMysql = 0;
    private int errorCountMongodb = 0;
    private int errorCountKafka = 0;

    @Scheduled(fixedRate = 10 * 1000) // 每20秒执行一次
    public void checkServiceStatus() {
        if(flag==0){
            log.info("=====基础服务状态监控未开启======");
            return;
        }

        checkMysql();
        if(errorCountMysql>=3){
            log.info("===========mysql服务异常，发送短信通知============");
            String msg = "服务mysql检测异常请及时查看"+ RandomUtil.randomString(3);
            sendCancelSms(Collections.singletonList(msg), notifyList);
        }
        checkMongodb();
        if(errorCountMongodb>=3){
            log.info("===========mongodb服务异常，发送短信通知============");
            String msg = "服务Mongodb检测异常请及时查看"+RandomUtil.randomString(3);
            sendCancelSms(Collections.singletonList(msg), notifyList);
        }
        checkKafka();
        if(errorCountKafka>=3){
            log.info("===========kafka服务异常，发送短信通知============");
            String msg = "服务kafka检测异常请及时查看"+RandomUtil.randomString(3);
            sendCancelSms(Collections.singletonList(msg), notifyList);
        }
    }

    /**
     * 发送下线通知
     * @param param
     * @param notifyList
     * @throws Exception
     */
    private void sendCancelSms(List<String> param,List<String> notifyList){
        for (String phone : notifyList) {
            sendSms(param, phone, 8);
        }
    }

    @Async
    public void sendSms(List<String> param,String phone, Integer type) {
        if(flag==0){
            log.info("=====服务状态监控发送短信未开启======");
            return;
        }
        try {
            smsProvider.sendMessageByTempId(phone,param,type);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 检测mysql
     * @return
     */
    private void checkMysql(){
        try{
            log.info("=====检测mysql服务======");
            mysqlClient.client();
            log.info("=====mysql服务正常======");
            errorCountMysql = 0;
        }catch (Exception e){
            log.info("=============检测mysql服务异常==========");
            errorCountMysql++;
        }
    }

    private void checkMongodb(){
        try{
            log.info("=====检测mongodb服务======");
            boolean flag = mongodbClient.client();
            if(!flag){
                log.info("=============检测Mongodb服务异常==========");
                errorCountMongodb++;
            }else{
                log.info("=====mongodb服务正常======");
                errorCountMongodb = 0;
            }
        }catch (Exception e){
            log.info("=============检测Mongodb服务异常==========");
            errorCountMongodb++;
        }
    }

    private void checkKafka(){
        try{
            log.info("=====检测kafka服务======");
            boolean flag = kafkaClient.client();
            if(!flag){
                log.info("=============检测Kafka服务异常==========");
                errorCountKafka++;
            }else{
                log.info("=====kafka服务正常======");
                errorCountKafka = 0;
            }
        }catch (Exception e){
            log.info("=============检测Kafka服务异常==========");
            errorCountKafka++;
        }
    }

    @Value("#{'${server.monitor.notify:}'.split(',')}")
    public void setNotifyList(List<String> notifyList) {
        this.notifyList = notifyList;
    }

}
